package model.proxy
{
	import model.Seance;
	
	import mx.rpc.AsyncToken;
	import mx.rpc.Responder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;
	
	import org.puremvc.as3.interfaces.IProxy;
	import org.puremvc.as3.patterns.proxy.Proxy; 
	
	public class SeanceProxy extends Proxy implements IProxy 
	{
		public static const NAME 							: String = 'SeanceProxy';		
		public static const LISTE_SEANCES_OBTENUE 			: String = "ListeSeancesObtenue";
		public static const LISTE_SEANCES_MAJ				: String = "ListeSeancesMaj";
		public static const SEANCE_AJOUTEE 					: String = "SeanceAjoutee";
		public static const LISTE_MATIERES_SEANCE_OBTENUE	: String = "ListeMatieresSeanceObtenue";
		public static const LISTE_SERVICES_SEANCE_OBTENUE	: String = "ListeServicesSeanceObtenue";
		public static const LISTE_GROUPES_SEANCE_OBTENUE	: String = "ListeGroupesSeanceObtenue";
		public static const LISTE_SALLES_SEANCE_OBTENUE		: String = "ListeSallesSeanceObtenue";
		public static const SEMAINE_PUBLIEE 				: String = "SemainePubliee";
		
		private var remoteObjectSchedule : RemoteObject = null;
		private var remoteObjectPublisher : RemoteObject = null;
		private var remoteObjectMatiere : RemoteObject = null;
		private var remoteObjectService : RemoteObject = null;
		private var remoteObjectGroupe 	: RemoteObject = null;
		private var remoteObjectSalle 	: RemoteObject = null;
		
		/** 
		 * Constructor 
		 * We create the table logins and insert a login by default to test 
		 */ 
		public function SeanceProxy(data:Object=null) 
		{
			super(NAME, data);
			
			remoteObjectSchedule = new RemoteObject();
			remoteObjectSchedule.destination = "RemoteServiceSchedule";
			remoteObjectSchedule.source = "RemoteServiceSchedule";
			remoteObjectSchedule.endpoint = ApplicationFacade.SERVEUR;
			
			remoteObjectMatiere = new RemoteObject();
			remoteObjectMatiere.destination = "RemoteServiceMatiere";
			remoteObjectMatiere.source = "RemoteServiceMatiere";
			remoteObjectMatiere.endpoint = ApplicationFacade.SERVEUR;
			
			remoteObjectService = new RemoteObject();
			remoteObjectService.destination = "RemoteServiceService";
			remoteObjectService.source = "RemoteServiceService";
			remoteObjectService.endpoint = ApplicationFacade.SERVEUR;
			
			remoteObjectGroupe = new RemoteObject();
			remoteObjectGroupe.destination = "RemoteServiceGroupeEtudiants";
			remoteObjectGroupe.source = "RemoteServiceGroupeEtudiants";
			remoteObjectGroupe.endpoint = ApplicationFacade.SERVEUR;

			remoteObjectSalle = new RemoteObject();
			remoteObjectSalle.destination = "RemoteServiceSalle";
			remoteObjectSalle.source = "RemoteServiceSalle";
			remoteObjectSalle.endpoint = ApplicationFacade.SERVEUR;
			
			remoteObjectPublisher = new RemoteObject();
			remoteObjectPublisher.destination = "RemoteServicePublisher";
			remoteObjectPublisher.source = "RemoteServicePublisher";
			remoteObjectPublisher.endpoint = ApplicationFacade.SERVEUR;
		}
 	
 		public function obtenirListeSeances(param:Object) : void
 		{
 			var token:AsyncToken = remoteObjectSchedule.obtenirListeSeances(param["anneeEtude"]);
			token.addResponder(new Responder(listeSeancesObtenue, onFault));	
			token.param = param;
 		}
		
		public function creerSeance(seance:Seance) : void
		{
			remoteObjectSchedule.addEventListener(ResultEvent.RESULT, listeSeancesMaj);
			remoteObjectSchedule.addEventListener(FaultEvent.FAULT, onFault);
			remoteObjectSchedule.creerSeance(seance);
		}
		
		public function modifierSeance(seance:Seance) : void
		{
			remoteObjectSchedule.addEventListener(ResultEvent.RESULT, listeSeancesMaj);
			remoteObjectSchedule.addEventListener(FaultEvent.FAULT, onFault);
			remoteObjectSchedule.modifierSeance(seance);
		}
		
		public function supprimerSeance(seance:Seance) : void
		{
			remoteObjectSchedule.addEventListener(ResultEvent.RESULT, listeSeancesMaj);
			remoteObjectSchedule.addEventListener(FaultEvent.FAULT, onFault);
			remoteObjectSchedule.supprimerSeance(seance);			
		}
		
		public function publierSemaine(param:Object) : void
		{
			remoteObjectPublisher.addEventListener(ResultEvent.RESULT, semainePubliee);
			remoteObjectPublisher.addEventListener(FaultEvent.FAULT, onFault);
			remoteObjectPublisher.publierSemaine(param["anneeEtude"], (param["jour"] as Date).time / 1000);				
		}
		/************************** RESULTAT ******************************/
		private function listeSeancesObtenue(event:ResultEvent):void
		{
			var result:Object = new Object();
			result["listeSeances"] = event.token.result as Array;
			result["calendarPanel"] = event.token.param["calendarPanel"];
			
			remoteObjectSchedule.removeEventListener(ResultEvent.RESULT, listeSeancesObtenue);
			sendNotification(LISTE_SEANCES_OBTENUE, result);
		}
		
		private function listeSeancesMaj(event:ResultEvent):void
		{
			remoteObjectSchedule.removeEventListener(ResultEvent.RESULT, listeSeancesMaj);
			var listeSeances:Array = (event.result as Array);
			sendNotification(LISTE_SEANCES_MAJ, listeSeances);
		}
		
		private function semainePubliee(event:ResultEvent):void
		{
			remoteObjectPublisher.removeEventListener(ResultEvent.RESULT, semainePubliee);
			sendNotification(SEMAINE_PUBLIEE);
		}
		
		private function onFault(event:FaultEvent):void
		{
			trace("Fault \n");
		}
				
		/******************************* POP-UP ***********************************/
		public function obtenirListeMatieres(param:Object):void
		{
			var token:AsyncToken = remoteObjectMatiere.obtenirListeMatieres(param["AnneeEtude"]);
			token.addResponder(new Responder(listeMatieresObtenue, onFault));	
			token.param = param;
		}
		
		private function listeMatieresObtenue(event:ResultEvent):void
		{
			var result:Object = new Object();
			result["ListeMatieres"] = event.token.result as Array;
			result["PopUp"] = event.token.param["PopUp"];
			sendNotification(SeanceProxy.LISTE_MATIERES_SEANCE_OBTENUE, result);
		}
		
		public function obtenirListeServices(param:Object):void
		{
			var token:AsyncToken = remoteObjectService.obtenirListeServices(param["Matiere"], param["AnneeEtude"]);
			token.addResponder(new Responder(listeServicesObtenue, onFault));	
			token.param = param;			
		}
		
		private function listeServicesObtenue(event:ResultEvent):void
		{
			var result:Object = new Object();
			result["ListeServices"] = event.token.result as Array;
			result["PopUp"] = event.token.param["PopUp"];
			sendNotification(SeanceProxy.LISTE_SERVICES_SEANCE_OBTENUE, result);			
		}
		
		public function obtenirListeGroupes(param:Object):void
		{
			var token:AsyncToken = remoteObjectGroupe.obtenirListeGroupesEtudiants(param["AnneeEtude"]);
			token.addResponder(new Responder(listeGroupesObtenue, onFault));	
			token.param = param;				
		}
		
		private function listeGroupesObtenue(event:ResultEvent):void
		{
			var result:Object = new Object();
			result["ListeGroupes"] = event.token.result as Array;
			result["PopUp"] = event.token.param["PopUp"];
			sendNotification(SeanceProxy.LISTE_GROUPES_SEANCE_OBTENUE, result);			
		}
		
		public function obtenirListeSalles(param:Object):void
		{
			var token:AsyncToken = remoteObjectSalle.obtenirListeSalles();
			token.addResponder(new Responder(listeSallesObtenu, onFault));	
			token.param = param;			
		}
		
		private function listeSallesObtenu(event:ResultEvent):void
		{
			var result:Object = new Object();
			result["ListeSalles"] = event.token.result as Array;
			result["PopUp"] = event.token.param["PopUp"];
			sendNotification(SeanceProxy.LISTE_SALLES_SEANCE_OBTENUE, result);					
		}
	}
}